package com.shcz.migu.utils;


import android.graphics.Bitmap;
import android.graphics.Matrix;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class PathUtil {
    /**
     * 創建文件夾
     *
     * @param path
     */
    public static void makeDirs(String path) {
        File file = new File(path);
        if (!file.exists() && !file.isFile()) {
            file.mkdirs();
        }
    }

    /**
     * 获取包含后缀名的文件名称
     *
     * @param url
     * @return
     */
    public static String getHttpFileName(String url) {
        File file = new File(url);
        return file.getName();
    }

    /**
     * 创建新文件
     *
     * @param fullPath
     */
    public static File createnNewFile(String fullPath) {
        File file = new File(fullPath);
        String dir = fullPath.substring(0, fullPath.lastIndexOf("/") + 1);
        File tempFile = new File(dir);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } finally {

            }
        }
        return file;
    }

    /**
     * 获取后缀名
     *
     * @param allname
     * @return
     */
    public static String getExtName(String allname) {
        if ((allname != null) && (allname.length() > 0)) {
            int i = allname.lastIndexOf('.');
            if ((i > -1) && (i < (allname.length() - 1))) {
                return ("." + allname.substring(i + 1));
            }
        }
        return null;
    }

    /**
     * 检查是否为图片地址
     *
     * @param path
     * @return
     */
    public static boolean checkPhoto(String path) {
        try {
            String prefix = path.substring(path.lastIndexOf(".") + 1);
            prefix = prefix.toLowerCase();
            if (prefix.equals("jpg") || prefix.equals("png") || prefix.equals("jpeg") || prefix.equals("bnp") || prefix.equals("gif")) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public static void saveMyBitmap(File bitName, Bitmap mBitmap) {
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(bitName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Matrix m = new Matrix();
            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();
            mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, m, true);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存到sdcard
     *
     * @param b
     * @return
     */
    public static String savePic(Bitmap b) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",
                Locale.US);
        String fname = CommonUtils.getAppCacheDirPath() + sdf.format(new Date()) + ".png";
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fname);
            if (null != fos) {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            fname = null;
        } catch (IOException e) {
            e.printStackTrace();
            fname = null;
        }
        return fname;
    }

    /**
     * 保存文件
     *
     * @param bitmapPath
     * @param fileName
     * @throws IOException
     */
    public static void saveFile(String bitmapPath, String fileName) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            inBuff = new BufferedInputStream(new FileInputStream(bitmapPath));
            outBuff = new BufferedOutputStream(new FileOutputStream(fileName));
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            outBuff.flush();
        } finally {
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    }

    /**
     * 删除单个文件
     *
     * @param sPath 被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除  
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }
}

